package com.loopy.fruns.web;

import com.loopy.fruns.entity.Cart;
import com.loopy.fruns.entity.Member;
import com.loopy.fruns.entity.Order;
import com.loopy.fruns.entity.OrderItem;
import com.loopy.fruns.service.OrderService;
import com.loopy.fruns.service.impl.OrderServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BasicServlet {
    private OrderService orderService=new OrderServiceImpl();
    //todo
    protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        Member member = (Member)request.getSession().getAttribute("member");
        if(null==cart||cart.isEmpty()){
            request.getRequestDispatcher("/index.jsp").forward(request,response);
            return;
        }
        if(null==member){
            request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
            return;
        }
        String orderId = orderService.saveOrder(cart, member.getId());

        request.getSession().setAttribute("orderId",orderId);

        response.sendRedirect(request.getContextPath()+"/views/order/checkout.jsp");
    }


    protected void queryOrderById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        Order order = orderService.queryOrderById(orderId);
//        if(null==order){
//todo
//        }
        request.getSession().setAttribute("order",order);
        request.getRequestDispatcher("/views/order/order.jsp").forward(request,response);
    }

    protected void getOrderItemListByOrderId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        List<OrderItem> orderItemLists = orderService.getOrderItemListByOrderId(orderId);
//        if(null==orderItemLists){
//todo
//        }
        request.getSession().setAttribute("orderItem",orderItemLists);
        request.getRequestDispatcher("/views/order/order_detail.jsp").forward(request,response);
    }

}
